#include <QtGui>
#include <qwt_slider.h>

#include "MediaPlayer.h"
#include "DiffPage.h"

#define FRAME_RATE 25

DiffPage::DiffPage(QWidget* parent):
    QWidget(parent)
{
    m_status =0;
    m_factor = 1.0;

    createActions();

    video = new VideoWidget;
    video2 = new VideoWidget;
    QHBoxLayout *hlayout1 = new QHBoxLayout;
    hlayout1->addWidget(video);
    hlayout1->addWidget(video2);
    //slider = new QSlider(Qt::Horizontal);
    slider = new QwtSlider(this, Qt::Horizontal/*,
                           QwtSlider::BottomScale,QwtSlider::Groove*/);
    slider->setHandleSize(10,20);
    slider->setRange(0,0);
    slider->setEnabled(false);
    //slider->setScaleMaxMajor(10);
    //slider->setScaleMaxMinor(5);
    QVBoxLayout * layout= new QVBoxLayout;
    QHBoxLayout * hlayout= new QHBoxLayout;
    layout->addLayout(hlayout1);
    hlayout->addWidget(slider,15);

    timeLabel = new QLCDNumber(5);
    timeLabel->display("0");
    hlayout->addWidget(timeLabel,1);
    layout->addLayout(hlayout);

    QToolBar *playBar = new QToolBar("play");
    //playBar->addAction(openAct);
    playBar->addAction(pauseAct);
    playBar->addAction(prevAct);
    playBar->addAction(nextAct);

    QHBoxLayout * hlayout2= new QHBoxLayout;
    hlayout2->addStretch();
    hlayout2->addWidget(playBar);
    hlayout2->addStretch();
    layout->addLayout(hlayout2);

    setLayout(layout);

    m_timer = new QTimer(this);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(update()));
    connect(slider,SIGNAL(valueChanged(double)),this,SLOT(moveVideo(double)));
    connect(video, SIGNAL(durationChanged(double)), this, SLOT(changeDuration(double)));
    connect(video, SIGNAL(posChanged(double)), this, SLOT(changePos(double)));
    connect(video, SIGNAL(sizeChanged(int,int)), this, SLOT(changeSize(int,int)));
    connect(video, SIGNAL(playFile(QString)), this, SLOT(openFile(QString)));
    connect(video2, SIGNAL(playFile(QString)), this, SLOT(openFile2(QString)));

    connect(video->horizontalScrollBar(),SIGNAL(valueChanged(int)),this,SLOT(moveHori(int)));
    connect(video->verticalScrollBar(),SIGNAL(valueChanged(int)),this,SLOT(moveVert(int)));
}

DiffPage::~DiffPage()
{
    stopPlay();
    delete m_timer;
}

void DiffPage::createActions()
{
    pauseAct = new QAction(QIcon(":/images/pause.png"),tr("P&ause"),this);
    pauseAct->setCheckable(true);
    connect(pauseAct, SIGNAL(toggled(bool)), this, SLOT(pause(bool)));
    pauseAct->setEnabled(false);

    prevAct = new QAction(QIcon(":/images/prev.png"),tr("&Prev Frame"),this);
    prevAct->setEnabled(false);
    connect(prevAct, SIGNAL(triggered()), this, SLOT(prevFrame()));
    nextAct = new QAction(QIcon(":/images/next.png"),tr("&Next Frame"),this);
    nextAct->setEnabled(false);
    connect(nextAct, SIGNAL(triggered()), this, SLOT(nextFrame()));
}

void DiffPage::scale(double fac,double oldFac)
{
    int width,height;
    width = m_width*fac;
    height = m_height*fac;
    video->changeSize(width,height);
    video2->changeSize(width,height);

    m_factor = fac;
}

void DiffPage::openFile(QString filename)
{
    if(filename.endsWith(".avi"))
    {
        pauseAct->setEnabled(true);
        pauseAct->setChecked(true);
        prevAct->setEnabled(true);
        nextAct->setEnabled(true);
	slider->setValue(0);
        //m_timer->start(100);
    }
    video->open(filename);
}

void DiffPage::openFile2(QString filename)
{
    if(filename.endsWith(".avi"))
    {
        pauseAct->setEnabled(true);
        pauseAct->setChecked(true);
        prevAct->setEnabled(true);
        nextAct->setEnabled(true);

        //m_timer->start(100);
    }
    video2->open(filename);
}

void DiffPage::stopPlay()
{
    video->stop();
    video->changeSize(1,1);
    video2->stop();
    video2->changeSize(1,1);
    m_timer->stop();
    pauseAct->setEnabled(false);
    pauseAct->setChecked(false);
    prevAct->setEnabled(false);
    nextAct->setEnabled(false);
    slider->setValue(0.0);
    slider->setRange(0,0);
}

void DiffPage::pause(bool type)
{
    video->togglePause();
    video2->togglePause();
    if(type) { 
		m_timer->stop(); 
		pauseAct->setIcon(QIcon(":/images/preview.png"));
	} else { 
		m_timer->start(100); 
		pauseAct->setIcon(QIcon(":/images/pause.png"));
	}
}

void DiffPage::moveVideo(double value)
{
  timeLabel->display(QString("%1").arg(value));
  if(m_status) { 
  	m_status=0; 
  }else {
    video->seekTo(value * FRAME_RATE);
    video2->seekTo(value * FRAME_RATE);
  }
}

void DiffPage::update()
{
    video->getTimePos();
    video2->getTimePos();
}

void DiffPage::changeDuration(double value)
{
    slider->setEnabled(true);
    slider->setRange(0,value / FRAME_RATE, 0.04);
    m_duration = value;
}

void DiffPage::changePos(double value)
{
  m_status = 1;
  slider->setValue(value / FRAME_RATE);
}

void DiffPage::changeSize(int width,int height)
{
    m_width = width;
    m_height = height;
    scale(m_factor, m_factor);
}

void DiffPage::prevFrame()
{
  double t = slider->value();
  t-=1;
  if(t>=0) { 
  	slider->setValue(t); 
  }else {
	slider->setValue(0); 
  }
}

void DiffPage::nextFrame()
{
  double t = slider->value();
  t+=1;
  if(t<=m_duration) { 
  	slider->setValue(t); 
  }else {
	slider->setValue(m_duration); 
  }
}

void DiffPage::moveHori(int value)
{
    video2->horizontalScrollBar()->setValue(value);
}

void DiffPage::moveVert(int value)
{
    video2->verticalScrollBar()->setValue(value);
}
